

var UsersModel = require('../Dao/userdao');
var JsonModel = require('../config/jsonModel');
var CheckLogin = require('./checkLogin');
var MD5S = require('../tool/MD5S');

module.exports = function(app) {

    app.post('/addUser', function (req, res) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var username = req.body.username;
        var passwords = req.body.passwords;
        var repasswords = req.body.repasswords;

        try {
            if (!(username.length >= 1 && username.length < 10)) {
                throw new Error('用户名限制在1-10个字符!');
            }
            if (!passwords) {
                throw new Error('密码不能为空!');
            }
            if (passwords !== repasswords) {
                throw new Error('两次输入的密码不一致!');
            }

        } catch (e) {
            // fs.unlink(req.files.avatar.path);
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        var user = {
            username: username,
            passwords: passwords,
        };
        UsersModel.addUser(user)
            .then(function (result) {
                var ss = result.ops[0];
                if (!ss) {
                    model.content = {};
                    model.status = 0;
                    model.msg = '未知错误，添加失败!';
                    res.send(model);
                } else {
                    model.content = {};
                    model.status = 1;
                    model.msg = '添加成功!';
                    res.send(model);
                }
            }).catch(function (error) {
            model.content = {};
            console.log(error);
            if (error.message.match('E11000 duplicate key')) {
                model.msg = '用户名重复!';
            } else {
                model.msg = error.message;
            }
            res.send(model);
        });
    });

    app.post('/userLogin', function (req, res) {
        var username = req.body.username;
        var passwords = req.body.passwords;
        var deviceType = req.body.deviceType;
        var model = new JsonModel.JsonModel();
        model.status = 0;

        try {
            if (!username) {
                throw new Error('用户名不能为空!');
            }
            if (!passwords) {
                throw new Error('密码不能为空!');
            }
            if (!deviceType) {
                throw new Error('设备类型不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }

        UsersModel.getUserByName(username).then(function (result) {
            var user = result[0];
            if (!user) {
                model.content = {};
                model.status = 0;
                model.msg = '没有该用户！';
                return res.send(model);
            }
            if (username === user.username && passwords == user.passwords) {
                var data = {};
                var token = MD5S.md5token(user._id);
                if (deviceType.toLowerCase() === 'web') {
                    data['webLogin'] = token;
                    data['webLoginTime'] = new Date().getTime().toString();
                    model.content = {username: user.username, sessionID: token, level: user.level};
                } else if (deviceType.toLowerCase() === 'ios') {
                    data['iOSLogin'] = token;
                    data['iOSLoginTime'] = new Date().getTime().toString();
                    model.content = {username: user.username, sessionID: token, level: user.level};
                } else if (deviceType.toLowerCase() === 'android') {
                    data['androidLogin'] = token;
                    data['androidLoginTime'] = new Date().getTime().toString();
                    model.content = {username: user.username, sessionID: token, level: user.level};
                } else {
                    model.content = {};
                    model.msg = '设备类型不正确!';
                    return res.send(model);
                }
                UsersModel.updateLoginStatus(user._id, data).then(function () {
                    model.status = 1;
                    model.msg = '登录成功!';
                    return res.send(model);
                }).catch(function (error) {
                    model.content = {};
                    model.msg = error.message;
                    return res.send(model);
                });
            } else {
                model.content = {};
                model.status = 0;
                model.msg = '用户名或密码不正确!';
                return res.send(model);
            }
        });
    });

    app.get('/getUserInfo', CheckLogin.checkUserLogin, function (res, req) {

    });

}